#ifndef _BROKER_H
#define _BROKER_H

#include "pollmgr.h"
#include "messaging.h"
#include "view.h"
#include "broker_protocol.pb.h"

#include <pthread.h>
#include <string>
#include <list>
#include <vector>

class cvmfs_broker : public view_observer {
public:
    cvmfs_broker(const std::string &primary, const std::string &id,
            const std::vector<std::string> &opts);
    ~cvmfs_broker();

private:
    void poll_loop();
    void output_loop();
    void heartbeater();

    void process_new_plugin(int fd);
    void process_plugin_msg(int fd);

    void internal_notify(const std::string &subject, const std::string &body);

    void previewchange(unsigned vid);
    void postviewchange(unsigned oldvid, unsigned newvid,
            const std::string &primary,
            const std::vector<std::string> &cohorts);

    std::string id_;
    int usock_fd_;
    pollmgr *poller_;
    int pipe_fd_[2];
    std::string sock_name_;

    std::list<DataMessage *> out_q_;
    std::list<fd_subscriber> plugin_subscribers_;
    std::set<int> plugin_fds_;

    pthread_mutex_t sub_m_;
    pthread_mutex_t out_m_;
    pthread_cond_t out_cv_;

    pthread_t poll_th_;
    pthread_t output_th_;

    messaging hermes_;
    viewmgr vmgr_;
};

#endif
